//
//  GFHomeFlowLayout.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFHomeFlowLayout.h"

@implementation GFHomeFlowLayout
CGFloat PickerFlowLayoutItemSize = 65;
CGFloat PickerFlowLayoutActiveDistance = 65;
CGFloat PickerFlowLayoutZoomFactor = 0.2;

- (instancetype)init {
    self = [super init];
    if (self) {
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.itemSize = CGSizeMake(ScreenWidth / 375 * 95, ScreenHeight - ScreenWidth / 375 * 500);
        self.minimumInteritemSpacing = ScreenWidth / 375 * 25;
        self.minimumLineSpacing = ScreenWidth / 375 * 25;
        self.sectionInset = UIEdgeInsetsMake(ScreenWidth / 375 * 20, ScreenWidth / 375 * 25, ScreenWidth / 375 * 20, ScreenWidth / 375 * 25);
    }
    return self;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}

#pragma mark _ 缩放大小
- (CGFloat)zoomFactor {
    return  1 + PickerFlowLayoutZoomFactor;
}

#pragma mark _ 展示的图片缩放方法
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSArray *layoutAttributes = [super layoutAttributesForElementsInRect:rect];
    
    CGRect visibleRect;
    visibleRect.origin = self.collectionView.contentOffset;
    visibleRect.size = self.collectionView.bounds.size;
    
    for (UICollectionViewLayoutAttributes *attributes in layoutAttributes) {
        if (CGRectIntersectsRect(attributes.frame, rect)) {
            CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
            CGFloat normalizedDistance = distance / PickerFlowLayoutActiveDistance;
            
            if (ABS(distance) < PickerFlowLayoutActiveDistance) {
                
                /* ABS 是取绝对值的方法 */
                CATransform3D transform = attributes.transform3D;
                transform = CATransform3DMakeTranslation(0, (1 - ABS(normalizedDistance))*-20, 0);
                
                CGFloat zoom = 1 + PickerFlowLayoutZoomFactor * (1 - ABS(normalizedDistance));
                attributes.transform3D = CATransform3DScale(transform, zoom, zoom, 1);
                
                attributes.zIndex = round(zoom);
                
                
                
                
            }
        }
    }
    
    return layoutAttributes;
    
}

#pragma mark _ 判断中间位置
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
    
    CGFloat offsetAdjustment = MAXFLOAT;
    CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
    
    CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    NSArray *layoutAttributes = [super layoutAttributesForElementsInRect:targetRect];
    
    for (UICollectionViewLayoutAttributes *attributes in layoutAttributes) {
        CGFloat itemHorizontalCenter = attributes.center.x;
        if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
            offsetAdjustment = itemHorizontalCenter - horizontalCenter;
        }
    }
    
    return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
    
}

@end
